package com.social.instadownloader.util;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

public class UniversalImageLoader extends ImageLoader{

	/** The instance. */
	private volatile static UniversalImageLoader INSTANCE = null;
	
	/**
	 * Instantiates a new universal image loader.
	 */
	protected UniversalImageLoader() {
	}
	
	/**
	 * Gets the single instance of UniversalImageLoader.
	 *
	 * @return single instance of UniversalImageLoader
	 */
	public static UniversalImageLoader getInstance(){
		if (INSTANCE == null) {
			synchronized (UniversalImageLoader.class) {
				if (INSTANCE == null) {
					INSTANCE = new UniversalImageLoader();
				}
			}
		}
		return INSTANCE;
	}
	
	/**
	 * Load image view.
	 *
	 * @param v the v
	 * @param imageUri the image uri
	 * @param regWidth the reg width
	 * @param regHeight the reg height
	 * @param options the options
	 * @param type the type
	 * @param listener the listener
	 * @param isShowLoadingDialog the is show loading dialog
	 */
	public void loadImageView(final View[] v,String imageUri, final int regWidth, final int regHeight) {
			final ProgressDialog mDialog = new ProgressDialog(v[0].getContext());
			mDialog.setMessage("Loading Image...");
			mDialog.setCancelable(false);
		loadImage(imageUri, convertToImageSize(regWidth, regHeight), IDisplayImageOption.defaultOption, new ImageLoadingListener() {
			
			@Override
			public void onLoadingStarted(String imageUri, View view) {
				if(mDialog!=null && !mDialog.isShowing()){
					mDialog.show();
				}
			}
			
			@Override
			public void onLoadingFailed(String imageUri, View view,
					FailReason failReason) {
				if(mDialog!=null && mDialog.isShowing()){
					mDialog.dismiss();
				}
			}
			
			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				if(mDialog.isShowing()){
					mDialog.dismiss();
				}
				for(int i=0;i<v.length;i++){
					View vg = v[i];
					if(vg!=null){
						setBackgroundBitmap(vg, loadedImage);
					}
				}
			}
			
			@Override
			public void onLoadingCancelled(String imageUri, View view) {
				if(mDialog.isShowing()){
					mDialog.dismiss();
				}
			}
		});
	}
	
	/**
	 * Convert to image size.
	 *
	 * @param width the width
	 * @param height the height
	 * @return the image size
	 */
	protected ImageSize convertToImageSize(int width, int height) {
		ImageSize size = new ImageSize(width, height);
		return size;
	}

	/**
	 * Convert to image size.
	 *
	 * @param width the width
	 * @return the image size
	 */
	protected ImageSize convertToImageSize(int width) {
		return convertToImageSize(width, 0);
	}
	
	/**
	 * Sets the background bitmap.
	 *
	 * @param v the v
	 * @param bitmap the bitmap
	 */
	protected void setBackgroundBitmap(View v,Bitmap bitmap){
		if(bitmap!=null){
			if(v instanceof ImageView){
				((ImageView)v).setImageBitmap(bitmap);
				return;
			}
			Drawable d = new BitmapDrawable(bitmap);
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
	            v.setBackground(d);
	        } else {
	            v.setBackgroundDrawable(d);
	        }
		}
	}
	
	/**
	 * The Interface IDisplayImageOption.
	 */
	public interface IDisplayImageOption {
		
		/** The default option. */
		DisplayImageOptions defaultOption 	= new DisplayImageOptions.Builder()
												.cacheInMemory(true)
												.cacheOnDisc(true)
												.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
//												.bitmapConfig(Bitmap.Config.RGB_565)
												.considerExifParams(true)
												.build();

		/** The animation option. */
		DisplayImageOptions animationOption = new DisplayImageOptions.Builder()
												.cacheInMemory(true)
												.cacheOnDisc(false)
												.considerExifParams(true)
												.bitmapConfig(Bitmap.Config.RGB_565)
												.build();

		/** The pager option. */
		DisplayImageOptions pagerOption 	= new DisplayImageOptions.Builder()
												.resetViewBeforeLoading(true)
												.cacheOnDisc(false)
												.imageScaleType(ImageScaleType.EXACTLY)
												.bitmapConfig(Bitmap.Config.RGB_565)
												.considerExifParams(true)
												.build();
	}
}
